
package com.jf.cloud.multishop.service.impl;

import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.util.PageUtil;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.multishop.constant.AuditStatus;
import com.jf.cloud.multishop.dto.ShopCompanyAuditingDTO;
import com.jf.cloud.multishop.dto.ShopCompanyDTO;
import com.jf.cloud.multishop.mapper.CompanyAuditingMapper;
import com.jf.cloud.multishop.model.ShopCompany;
import com.jf.cloud.multishop.model.ShopCompanyAuditing;
import com.jf.cloud.multishop.service.CompanyAuditingService;
import com.jf.cloud.multishop.service.ShopCompanyService;
import com.jf.cloud.multishop.vo.ShopCompanyAuditingVO;
import com.jf.cloud.multishop.vo.ShopCompanyVO;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * @author zz
 * @date 2022/9/13 14:40
 */

@Service
public class CompanyAuditingServiceImpl implements CompanyAuditingService {

    @Autowired
    private CompanyAuditingMapper companyAuditingMapper;
    @Autowired
    private ShopCompanyService shopCompanyService;
    @Autowired
    private MapperFacade mapperFacade;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void applyChangeCompanyInfo(ShopCompanyDTO shopCompanyDTO) {
        // 把之前审核不通过的工商信息删除
        shopCompanyService.deleteByShopIdAndStatus(shopCompanyDTO.getShopId(), AuditStatus.FAILAUDIT.value());
        // 保存变更工商信息
        ShopCompany shopCompany = mapperFacade.map(shopCompanyDTO, ShopCompany.class);
        shopCompany.setStatus(AuditStatus.WAITAUDIT.value());
        shopCompanyService.save(shopCompany, false);
        // 生成审核信息
        generateAuditInformation(shopCompany);
    }

    @Override
    public ShopCompanyAuditingVO getLatestAuditingByShopId(Long shopId) {
        return companyAuditingMapper.getLatestAuditingByShopId(shopId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void revoke(Long shopId) {

        ShopCompanyAuditingVO shopCompanyAuditingVO = getLatestAuditingByShopId(shopId);
        if (Objects.isNull(shopCompanyAuditingVO) || !Objects.equals(shopCompanyAuditingVO.getStatus(), AuditStatus.WAITAUDIT.value())) {
            throw new LuckException("申请信息未能找到，请刷新页面");
        }
        // 删除申请变更的工商信息
        shopCompanyService.deleteByShopIdAndStatus(shopId, AuditStatus.WAITAUDIT.value());
        // 删除申请审核信息
        companyAuditingMapper.deleteById(shopCompanyAuditingVO.getCompanyAuditingId());
    }

    @Override
    public PageVO<ShopCompanyAuditingVO> page(PageDTO pageDTO, ShopCompanyAuditingDTO shopCompanyAuditingDTO) {
        return PageUtil.doPage(pageDTO, () -> companyAuditingMapper.list(shopCompanyAuditingDTO));
    }

    @Override
    public ShopCompanyAuditingVO getAuditInfo(Long shopId) {
        ShopCompanyAuditingVO shopCompanyAuditingVO = getLatestAuditingByShopId(shopId);
        if (Objects.isNull(shopCompanyAuditingVO)) {
            return shopCompanyAuditingVO;
        }
        ShopCompanyVO shopCompanyVO = shopCompanyService.getById(shopCompanyAuditingVO.getShopCompanyId());
        shopCompanyAuditingVO.setShopCompanyVO(shopCompanyVO);
        return shopCompanyAuditingVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void audit(ShopCompanyAuditingDTO shopCompanyAuditingDTO) {
        ShopCompanyAuditing companyAuditing = companyAuditingMapper.getById(shopCompanyAuditingDTO.getCompanyAuditingId());
        if (Objects.isNull(companyAuditing) || !Objects.equals(companyAuditing.getStatus(), AuditStatus.WAITAUDIT.value())) {
            throw new LuckException("申请信息未能找到，请刷新页面");
        }
        companyAuditing.setAuditorId(AuthUserContext.get().getUserId());
        // 审核不通过
        if (Objects.equals(shopCompanyAuditingDTO.getStatus(), AuditStatus.FAILAUDIT.value())) {
            companyAuditing.setStatus(AuditStatus.FAILAUDIT.value());
            companyAuditing.setRemarks(shopCompanyAuditingDTO.getRemarks());
            companyAuditingMapper.update(companyAuditing);
            // 把对应的工商信息设置为审核未通过状态
            ShopCompany shopCompany = new ShopCompany();
            shopCompany.setShopCompanyId(companyAuditing.getShopCompanyId());
            shopCompany.setStatus(AuditStatus.FAILAUDIT.value());
            shopCompanyService.updateById(shopCompany);
            return;
        }
        // 审核通过(if不能省略，防止传入非法参数)
        if (Objects.equals(shopCompanyAuditingDTO.getStatus(), AuditStatus.SUCCESSAUDIT.value())) {
            companyAuditing.setStatus(AuditStatus.SUCCESSAUDIT.value());
            companyAuditingMapper.update(companyAuditing);

            shopCompanyService.batchUpdateStatus(companyAuditing.getShopId(), AuditStatus.FAILAUDIT.value());
            // 把对应的工商信息设置为启用状态
            ShopCompany shopCompany = new ShopCompany();
            shopCompany.setShopCompanyId(companyAuditing.getShopCompanyId());
            shopCompany.setStatus(AuditStatus.SUCCESSAUDIT.value());
            shopCompanyService.updateById(shopCompany);
        }
    }

    /**
     * 生成店铺申请记录
     *
     * @param shopCompany
     */
    private void generateAuditInformation(ShopCompany shopCompany) {
        ShopCompanyAuditing shopCompanyAuditing = new ShopCompanyAuditing();
        shopCompanyAuditing.setShopId(shopCompany.getShopId());
        shopCompanyAuditing.setShopCompanyId(shopCompany.getShopCompanyId());
        shopCompanyAuditing.setUserId(AuthUserContext.get().getUserId());
        shopCompanyAuditing.setStatus(AuditStatus.WAITAUDIT.value());
        companyAuditingMapper.insert(shopCompanyAuditing);
    }
}
